#include "loginwidget.h"
#include "ui_loginwidget.h"
#include "userwidget.h"
#include "registwidget.h"
#include "proto.h"

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);
    //调整窗口大小
    this->resize(500,400);
    this->setMaximumSize(450,300);
    this->setMinimumSize(450,300);

    //窗口属性
    this->setWindowTitle(QString("QQ"));
    QIcon windowIcon = QIcon(QPixmap(QString(":/new/prefix1/pic/QQ.jpg")));
    this->setWindowIcon(windowIcon);

    //背景图片
    QPalette pal;
    pal.setBrush(QPalette::Background,\
                 QBrush(QPixmap(QString(":/new/prefix1/pic/back.jpeg")).scaled(500,450)));
    this->setPalette(pal);
    
    //窗口组件
    label = new QLabel();
    label->resize(50,50);
    label->setPixmap(QPixmap(QString(":/new/prefix1/pic/hand.jpeg")).scaled(60,60));
    labelLayout = new QHBoxLayout();
    labelLayout->setContentsMargins(0,0,0,0);
    labelLayout->addStretch();
    labelLayout->addWidget(label);
    labelLayout->addStretch();
    
    //字体
    QFont font;
    font.setPixelSize(15);
    font.setBold(true);
    
    //用户名
    nameLineEdit = new QLineEdit();
    nameLineEdit->setPlaceholderText(QString("账号"));
    nameLineEdit->setFont(font);
    //密码
    passwordLineEdit = new QLineEdit();
    passwordLineEdit->setPlaceholderText(QString("密码"));
    passwordLineEdit->setFont(font);
    passwordLineEdit->setEchoMode(QLineEdit::Password);
    //自动登录
    autoLoginBox = new QCheckBox(QString("自动登录"));
    //记住密码
    rmbPasswordBox = new QCheckBox(QString("记住密码"));
    //找回密码
    findPasswordBtn = new QPushButton(QString("找回密码"));
    findPasswordBtn->setFlat(true);
    
    checkBoxLayout = new QHBoxLayout();
    checkBoxLayout->addWidget(autoLoginBox);
    checkBoxLayout->addWidget(rmbPasswordBox);
    checkBoxLayout->addWidget(findPasswordBtn);
    //登录
    loginBtn = new QPushButton(QString("登录"));
    loginBtn->setFont(font);
    //注册
    registBtn = new QPushButton(QString("注册账号"),this);
    registBtn->setGeometry(8,270,80,30);
    registBtn->setFlat(true);
    //二维码
    imgBtn = new QPushButton(this);
    imgBtn->setGeometry(410,270,40,30);
    imgBtn->setFlat(true);
    imgBtn->setIcon(QIcon(QPixmap(QString(":/new/prefix1/pic/屏幕截图 2021-06-28 164706.jpg"))));
    //垂直结构
    mainLayout = new QVBoxLayout();
    mainLayout->setContentsMargins(80,20,80,50);
    mainLayout->addLayout(labelLayout);
    mainLayout->addWidget(nameLineEdit);
    mainLayout->addWidget(passwordLineEdit);
    mainLayout->addLayout(checkBoxLayout);
    mainLayout->addWidget(loginBtn);
    this->setLayout(mainLayout);

    //报时套接字
    sd = new QUdpSocket();

    //链接信号与曹
    connect(registBtn,SIGNAL(clicked(bool)),this,SLOT(rgsBtnSlot()));
    connect(loginBtn,SIGNAL(clicked(bool)),this,SLOT(loginBtnSlot()));
    connect(sd,SIGNAL(readyRead()),this,SLOT(on_readyread_udp()));
}

void LoginWidget::rgsBtnSlot()
{
    registwidget *r = new registwidget();
    r->show();
}

void LoginWidget::loginBtnSlot()
{
    rgs_t sndbuf;

    QString cntstr = nameLineEdit->text();//获取选框的内容
    QString pwdstr = passwordLineEdit->text();

    strncpy(sndbuf.cntbuf,cntstr.toLatin1().data(),CNTSIZE);//将获取到的用户名复制到协议中
    QByteArray pwdbta = QCryptographicHash::hash(pwdstr.toLatin1(),QCryptographicHash::Sha512);//将口令原文加密
    strncpy(sndbuf.pwdbuf,pwdbta.toHex().data(),PWDSIZE);//将加密后的口令原文复制到协议中

    qDebug() << sndbuf.cntbuf <<sndbuf.pwdbuf ;

    sd->writeDatagram((char *)&sndbuf,sizeof(sndbuf),QHostAddress(RGS_IP),LOG_PORT);//打包数据包

    //userwidget *r = new userwidget();
    //r->show();
}

void LoginWidget::on_readyread_udp()
{
    rgs_t rcvbuf;
    userwidget *s = new userwidget();
    int ret;

    sd->readDatagram((char *)&rcvbuf,sizeof(rcvbuf));
    switch(rcvbuf.flag)
    {
    case LOG_OK:
        s->setAttribute(Qt::WA_DeleteOnClose);
        s->show();
        this->close();
        break;
    case LOG_EXISTS:
        ret = QMessageBox::information(this,QString("登录"),QString("密码输入错误，请重新输入"),QMessageBox::Yes,QMessageBox::No);
        if(ret == QMessageBox::No)
            this->close();
        else
        {
            passwordLineEdit->clear();
        }
        break;
    case LOG_ERROR:
        ret = QMessageBox::question(this,QString("登录"),QString("账号不存在，请注册账号再登录"),QMessageBox::Ok);
        break;
    default:
        break;
    }
}


LoginWidget::~LoginWidget()
{
    delete ui;
}
